<template>
	<view class="loginPage">
		<view class="loginPage-title">
			赤兔养车
		</view>
		<view class="loginPage-hello">
			您好,
		</view>
		<view class="loginPage-welcome">
			欢迎来到赤兔养车
		</view>

		<u-toast ref="showWrongtel" />
		<!-- 电话号码和验证码 -->
		<view class="confirmedtele">
			<view class="tele">
				<text class="tele-text">+86</text><input placeholder="请输入手机号" v-model="telephone" class="tele-ipt"
					type="text" @blur="checktelephone" />
			</view>
			<view class="confimedcode">
				<u-verification-code @change="codeChange" :seconds="seconds" @start="start" ref="sendCode">
				</u-verification-code>
				<input v-model="code" type="text" placeholder="请输入验证码" class="confimedcode-ipt" /><text
					class="confimedcode-text">|</text><text class="confimedcode-textsend" @tap="getCode">{{tips}}</text>
			</view>

		</view>

		<view class="loginbutton" @tap="login">
			登录
		</view>

		<view class="loginmethods">
			<navigator url="/pages/loginLx/loginLx"><text class="methoditem">密码登录</text></navigator>

			<navigator url="/pages/register/register"><text class="methoditem">立即注册</text></navigator>

		</view>
	</view>

</template>

<script>
	import {
		createNamespacedHelpers
	} from "vuex";
	let {
		mapActions,
		mapMutations
	} = createNamespacedHelpers("users");
	export default {
		data() {
			return {
				border: true,
				telephone: "",
				code: "",
				seconds: 60,
				tips: '获取验证码'
			}
		},
		methods: {
			...mapActions(["getusercode", "loginByTel"]),
			...mapMutations(["setUserInfo"]),
			// 点击登录跳转
			async login() {
				let data = await this.loginByTel({
					phoneNum: this.telephone,
					code: this.code
				})
				if (data.token) {
					//登录成功，保存token到本地
					uni.setStorageSync("token", data.token);
					uni.setStorageSync("id", data.custom.id); //保存用户id到本地
					this.setUserInfo(data.custom);
					uni.switchTab({
						url: '/pages/mine/mine'
					});
				} else {
					this.$refs.showWrongtel.show({
						title: '请输入已注册的手机号和正确的验证码',
						type: 'error',
						position: "top"
					})
				}

			},
			//失焦判断电话号码是否正确
			checktelephone() {
				let result = RegExp(/^1[34578]\d{9}$/).test(this.telephone);
				if (!result) {
					this.$refs.showWrongtel.show({
						title: '手机号码不正确',
						type: 'error',
						position: "top"
					})
				}
			},

			codeChange(text) {
				this.tips = text;
			},
			// 获取验证码
			getCode() {
				// 如果手机号码符合规范，才能发送验证码
				if (RegExp(/^1[34578]\d{9}$/).test(this.telephone)) {
					if (this.$refs.sendCode.canGetCode) {
						this.getusercode(this.telephone)
						// 模拟向后端请求验证码
						uni.showLoading({
							title: '正在获取验证码'
						})
						setTimeout(() => {
							uni.hideLoading();
							// 这里此提示会被this.start()方法中的提示覆盖
							this.$u.toast('验证码已发送');
							// 通知验证码组件内部开始倒计时
							this.$refs.sendCode.start();
						}, 2000);
					} else {
						this.$u.toast('倒计时结束后再发送');
					}
				}

			},
			// 倒计时开始
			start() {
				this.$u.toast('倒计时开始');
			}
		}
	}
</script>
<style scoped lang="scss">
	.loginPage {
		padding-bottom: 300rpx;
		background-color: white;

		.loginPage-title {
			margin: 0rpx auto;
			text-align: center;
			font-size: 46rpx;
			color: #f5610c;
			font-weight: 700;
			line-height: 200rpx;
		}

		.loginPage-hello {
			font-size: 50rpx;
			padding: 0rpx 50rpx;
			color: #f5610c;
		}

		.loginPage-welcome {
			font-size: 36rpx;
			padding: 0rpx 50rpx;
			color: #f5610c;
		}

		// 验证码和手机号
		.confirmedtele {
			width: 650rpx;
			margin: 40rpx auto;

			// 电话号码
			.tele {
				border-radius: 100rpx;
				height: 80rpx;
				background-color: #f2f2f2;
				margin-top: 150rpx;
				display: flex;

				.tele-text {
					line-height: 80rpx;
					margin-left: 30rpx;
				}

				.tele-ipt {
					line-height: 80rpx;
					width: 500rpx;
					height: 80rpx;
					margin-left: 20rpx;
				}
			}

			// 验证码
			.confimedcode {
				margin-top: 30rpx;
				border-radius: 100rpx;
				height: 80rpx;
				background-color: #f2f2f2;
				display: flex;

				.confimedcode-ipt {
					line-height: 80rpx;
					height: 80rpx;
					width: 400rpx;
					margin-left: 30rpx;
				}

				.confimedcode-text {
					height: 80rpx;
					line-height: 80rpx;
					margin-left: 20rpx;
					color: #bbbbbb;
				}

				.confimedcode-textsend {
					height: 80rpx;
					line-height: 80rpx;
					margin-left: 20rpx;
					color: #bbbbbb;
					width: 230rpx;
				}
			}
		}

		// 登录按钮
		.loginbutton {
			width: 650rpx;
			margin: 100rpx auto;
			margin-bottom: 30rpx;
			text-align: center;
			line-height: 80rpx;
			color: white;
			border-radius: 100rpx;
			background-color: #f5610c;
		}

		.loginmethods {
			display: flex;
			padding: 10rpx 60rpx;
			justify-content: space-between;

			.methoditem {
				color: #f5610c;
			}
		}

	}
</style>
